package com.lib.audioDecoder.player;

import com.lib.audioDecoder.AudioCodec;

public class AudioPlayer {

    private final PCMPlayer mPCMPlayer;
    private final AudioCodec mAudioCodec;
    private final AudioCodec.IDecodeCallback mDecodeCallback =
            new AudioCodec.IDecodeCallback() {
        @Override
        public void onFrame(byte[] data) {
            mPCMPlayer.play(data,data.length);
        }
    };

    public AudioPlayer(AUDIO_CODEC_TYPE type, int buffer,
                       int sampleRate,int sampleBit,int channelNum){
        this.mPCMPlayer = new PCMPlayer(buffer,sampleRate,channelNum);
        this.mAudioCodec = new AudioCodec(type);
        this.mAudioCodec.create(sampleRate,channelNum,sampleBit,
                mDecodeCallback);
    }

    public void play(byte[] data){
        play(data,0,data.length);
    }

    public void play(byte[] data,int offset,int size){
        mAudioCodec.decode(data,offset,size);
    }

    public synchronized void close(){
        mPCMPlayer.stop();
        mAudioCodec.close();
    }

}
